#include <QtGui>
#include "memoDetailInfoDialog.h"
#include "memoDetailInfoWidget.h"

MemoDetailInfoDialog::MemoDetailInfoDialog(QSqlDatabase _db,
                                           QString _strMemoID,
                                           DialogType _DialogType,
                                           QWidget *parent)
    :QDialog(parent)
{
    QHBoxLayout* _BtnsLayout = new QHBoxLayout;
    if(_DialogType == Show){
        //show
        m_pMemoDetailInfoWidget =
                new MemoDetailInfoWidget(_db,
                                         _strMemoID,
                                         MemoDetailInfoWidget::UNEDITABLE,
                                         this);
        //btns
        m_pBtnCancel = new QPushButton("Ok", this);
        connect(m_pBtnCancel,SIGNAL(clicked()),
                this,SLOT(reject()));
        _BtnsLayout->addStretch(2);
        _BtnsLayout->addWidget(m_pBtnCancel);
        _BtnsLayout->addStretch(2);

    }else if(_DialogType == New){
        //new
        m_pMemoDetailInfoWidget =
                new MemoDetailInfoWidget(_db,
                                         "",
                                         MemoDetailInfoWidget::EDITABLE,
                                         this);
        //btns
        m_pBtnAdd  = new QPushButton("Add", this);
        connect(m_pBtnAdd,SIGNAL(clicked()),
                this,SLOT(doBtnAdd()));
        m_pBtnCancel = new QPushButton("Cancel", this);
        connect(m_pBtnCancel,SIGNAL(clicked()),
                this,SLOT(reject()));
        _BtnsLayout->addStretch(2);
        _BtnsLayout->addWidget(m_pBtnAdd);
        _BtnsLayout->addWidget(m_pBtnCancel);
        _BtnsLayout->addStretch(2);


    }else if(_DialogType == Modify){
        //Modify
        m_pMemoDetailInfoWidget =
                new MemoDetailInfoWidget(_db,
                                         _strMemoID,
                                         MemoDetailInfoWidget::EDITABLE,
                                         this);
        //btns
        m_pBtnModify  = new QPushButton("Modify", this);
        connect(m_pBtnModify,SIGNAL(clicked()),
                this,SLOT(doBtnModify()));
        m_pBtnDel = new QPushButton("Delete", this);
        connect(m_pBtnDel,SIGNAL(clicked()),
                this,SLOT(doBtnDel()));
        m_pBtnCancel = new QPushButton("Cancel", this);
        connect(m_pBtnCancel,SIGNAL(clicked()),
                this,SLOT(reject()));
        _BtnsLayout->addStretch(2);
        _BtnsLayout->addWidget(m_pBtnModify);
        _BtnsLayout->addWidget(m_pBtnDel);
        _BtnsLayout->addWidget(m_pBtnCancel);
        _BtnsLayout->addStretch(2);

    }else if(_DialogType == Delete){
        //Del
        m_pMemoDetailInfoWidget =
                new MemoDetailInfoWidget(_db,
                                         _strMemoID,
                                         MemoDetailInfoWidget::UNEDITABLE,
                                         this);
        //btns
        m_pBtnDel = new QPushButton("Delete", this);
        connect(m_pBtnDel,SIGNAL(clicked()),
                this,SLOT(doBtnDel()));
        m_pBtnCancel = new QPushButton("Cancel", this);
        connect(m_pBtnCancel,SIGNAL(clicked()),
                this,SLOT(reject()));
        _BtnsLayout->addStretch(2);
        _BtnsLayout->addWidget(m_pBtnDel);
        _BtnsLayout->addWidget(m_pBtnCancel);
        _BtnsLayout->addStretch(2);
    }else{
        //Todo as show
        m_pMemoDetailInfoWidget =
                new MemoDetailInfoWidget(_db,
                                         _strMemoID,
                                         MemoDetailInfoWidget::UNEDITABLE,
                                         this);
        //btns
        m_pBtnCancel = new QPushButton("Ok", this);
        connect(m_pBtnCancel,SIGNAL(clicked()),
                this,SLOT(reject()));
        _BtnsLayout->addStretch(2);
        _BtnsLayout->addWidget(m_pBtnCancel);
        _BtnsLayout->addStretch(2);
    }

    QVBoxLayout* _DialogLayout = new QVBoxLayout;
    _DialogLayout->addWidget(m_pMemoDetailInfoWidget);
    _DialogLayout->addLayout(_BtnsLayout);
    this->setLayout(_DialogLayout);
}

void MemoDetailInfoDialog::doBtnAdd()
{
    if(m_pMemoDetailInfoWidget->addNewRecord()){
        this->accept();
    }
}

void MemoDetailInfoDialog::doBtnModify()
{
    if(m_pMemoDetailInfoWidget->updateRecord()){
        this->accept();
    }

}

void MemoDetailInfoDialog::doBtnDel()
{
    if(m_pMemoDetailInfoWidget->deleteRecord()){
        this->accept();
    }
}

QString MemoDetailInfoDialog::getID()
{
    return m_pMemoDetailInfoWidget->getID();
}

